//初始化列表数组
var List = [];
var startDate = '';
var endDate = '';
var editUserId = 0;
var rolelist = [];
//分页查询
var limit = 5;// 每页多少条
var currPage = 1;// 当前页
var allPage = 1;// 总页数
var count = 0;// 默认共多少条数据

var name = '';
var role = 0;
var roleid = 0;
var one = 0;
var tow = 0;
var three = 0;

getRoleListWithAccess();
function getRoleListWithAccess(){
	$.ajax({
		url:ajax_host+'getrolewithaccess.act',
        dataType:'JSON',
    	success:function(resp){
    		if(resp.success){
                rolelist = resp.list;
				console.log("列表："+rolelist);
				renderTypee(rolelist);
    		}else{
                layer.alert('获取角色失败')
            }
    	},
    	
    });
}
function renderTypee(arr){
    var str = '<option value="0">请选择</option>';
    for(var i = 0; i<arr.length;i++){
		str+=`<option  value="${arr[i].rId}">${arr[i].rName}</option>`;
	}
	html('roleid',str);
}
function changerole2(){
	roleid = val('roleid');
	roleid = $('#roleid').val();
    console.log('低角色id：'+roleid);
} 



//获得角色列表(查询表)
getrolelist();
function getrolelist(){
	$.ajax({
		url:ajax_host+'getrole.act',
        dataType:'JSON',
    	success:function(resp){
    		if(resp.success){
                List = resp.list;
				console.log("列表："+List);
				renderType(List);
    		}else{
                layer.alert('获取角色失败')
            }
    	},
    	
    });
}

function renderType(arr){
    var str = '<option value="0">请选择</option>';
    for(var i = 0; i<arr.length;i++){
		str+=`<option  value="${arr[i].rId}">${arr[i].rName}</option>`;
	}
	html('role',str);
}
function changerole(){
	role = val('role');
	role = $('#role').val();
    console.log('类型id：'+role);
} 


getaddressone();
function getaddressone(){
    console.log('开始获取省级列表')
    $.ajax({
		url : ajax_host +'getAdminaddressone.act',
		dataType:'JSON',
		
		success:function(resp){
			if(resp.success){
                addressonelist = resp.list;
                
				console.log("列表："+addressonelist);
				  renderAddressOne(addressonelist)
			}
		}
	})
}
function renderAddressOne(arr){
    var str = '<option value="0">请选择</option>';
    for(var i = 0; i<arr.length;i++){
		str+=`<option  value="${arr[i].nId}">${arr[i].nName}</option>`;
	}
	html('addressOne',str);
}
function change(){
    one = val('addressOne');
    console.log('one'+one);
    $.ajax({
		url : ajax_host +'getAdminaddressTow.act',
		dataType:'JSON',
		data:{
			fid : one
		},
		success:function(resp){		
			if(resp.success){
            addressTowlist = resp.list;
			renderAddressTow(addressTowlist);
		}else{
			layer.alert(resp.errmsg);
		}
		}
	})
}
function renderAddressTow(arr){
	var str = '<option value="0">请选择</option>';
	for(var i = 0; i<arr.length;i++){
		str+=`<option  value="${arr[i].nId}">${arr[i].nName}</option>`;
	}
	html('addressTwo',str);
}
function change2(){
     tow = val('addressTwo');
    console.log('tow'+tow);
    $.ajax({
		url : ajax_host +'getAdminaddressThree.act',
		dataType:'JSON',
		data:{
			fid : tow
		},
		success:function(resp){		
			if(resp.success){
             addressThreelist = resp.list;
			renderAddressThree(addressThreelist);
		}else{
			layer.alert(resp.errmsg);
		}
		}
	})
}
function renderAddressThree(arr){
	var str = '<option value="0">请选择</option>';
	for(var i = 0; i<arr.length;i++){
		str+=`<option  value="${arr[i].nId}">${arr[i].nName}</option>`;
	}
	html('addressThree',str);
}


function change3(){
     three = val('addressThree');
    console.log('three'+three);
    
}






function getstartDate(){
	startDate = document.getElementById('start').value;
	
	
}
function getendDate(){
	endDate = document.getElementById('end').value;
	
}
function doSend(){
	name = val('name');
	role = $('#role').val();
	getAdminlist();
}
getAdminRid();
function getAdminRid(){
	$.ajax({
		url:ajax_host+'getAdminRid.act',
    	dataType:'JSON',
    	success:function(resp){
    		if(resp.success){
				nowRid = resp.rid;
				console.log('resp.rid:'+resp.rid);
				console.log('ajax登陆者角色id:'+nowRid);
				getAdminlist();
    		}else{
				layer.alert('获取登陆者角色id失败');
			}
    	},
    	error:function(resp){
    		layer.alert('获取登陆者角色id失败');
    	}
	});
}

function getAdminlist(){
	console.log(startDate);
	console.log(endDate);


	$.ajax({
		url:ajax_host+'getAdminlist.act',
		dataType : 'JSON',
		data:{
			name : name,
			role : role,
			start : startDate,
			end : endDate,
			startpage : currPage,
			limitpage : limit
		},
		success:function(resp){
			
			if(resp.success){
				List = resp.list;
				console.log("查询后列表："+List);
				renderTable(resp.list);

				

				// 总页数=记录数/每页记录
				count = resp.count;
				console.log('总记录数:'+count)
				allPage=count%limit==0?(count/limit):(parseInt(count/limit+1));
				console.log('总页数:'+allPage);
				$("#pager").html(currPage+"/"+allPage);
			}else{
				layer.alert(resp.errmsg);
			}
        }
	})
}

function renderTable(arr){
	console.log('登录者角色id:'+nowRid)
	var str = `<tr>
		<th>管理员名</th>
		<th>角色</th>
		<th>状态</th>
		<th>操作</th>
		
	</tr>`;
	for(var i = 0;i<arr.length;i++){
		var a = arr[i];
		str += 
		   `<tr>
			<td>${a.adminName}</td>
			<td>${a.rName}</td>
			<td>${a.adminState == 0? '启用':'禁用'}</td>
			<td>`;
			if(a.roleId>nowRid){
			str +=
			`<a href = "###" onclick="doUpdate(${i},${a.adminId})">修改状态</a>
			<a href = "###" onclick="del(${i})">删除</a>
			<a href = "###" onclick="newpassword(${i})">重置密码</a>`;
			str+=`</td>`;
			}
			str+=`</tr>`;
		
	}
	html('table',str); 
}




function doUpdate(index,uId){
	console.log("遮罩获取列表："+List);
	
	console.log("获取的uid:"+uId);
	
    $('#doUpdate').modal({
        show:true,//展示出来
        moveable:true,//可移动
        rememberPos:true,//记住移动位置
		backdrop:'static'//背景遮罩
	});
	var user = List[index];
	editUserId = uId;
	console.log('user的值为'+user);
	console.log('edit的值为'+editUserId);
	//拿到单选框对象
	var radios = document.getElementsByName('status1');
	for(var i = 0; i<radios.length; i++){
		if(radios[i].value == user.state){//判断value和我们的值是否相等
			
			radios[i].checked = true;		
			
		}
	}
}

function confirm2(){
	// $('#doUpdate').modal('hide', 'fit');
	$.ajax({
		url:ajax_host+'adminEdit.act',
		type : 'post',
		dataType:'JSON',
		data:{
			
			status : val('status1','radio'),
			uid : editUserId
		},
		success:function(resp){
			
			if(resp.success){
				//关闭遮罩
				$('#doUpdate').modal('hide', 'fit');
				layer.alert('修改成功');
				getAdminlist();
			}else{
				$('#doUpdate').modal('hide', 'fit');
				layer.alert(resp.errmsg);
			}
		}
})
}

//给删除按钮挂事件
function del(index){
	console.log('列表删除'); 
	layer.confirm("确定删除该用户吗？", {
		btn: ['确定','取消'] //按钮
	   ,skin: 'layui-layer-lan'
	   ,closeBtn: 0
	   ,anim: 3 //动画类型
   },function(){

	var user = List[index];
	//空指针的判断
	console.log('判断'+user)
	if(!user || user.adminId <=0){
		return;
	}
	$.ajax({
		url:ajax_host+'adminDel.act',
		type:'post',
		dataType:'JSON',
		data:{
			uid : user.adminId
		},
	success:function(resp){
		if(resp.success){
			//删除数组的元素
			layer.alert('删除成功');
			
			getAdminlist();
			
		}else{
			layer.alert(resp.errmsg);
		}
		
	}
	})


   });
		
	
}

function newpassword(index){
	console.log('重置密码');
	layer.confirm("确定重置该用户密码吗？", {
		btn: ['确定','取消'] //按钮
	   ,skin: 'layui-layer-lan'
	   ,closeBtn: 0
	   ,anim: 3 //动画类型
   },function(){

	var user = List[index];
	//空指针的判断
	console.log('判断'+user)
	if(!user || user.adminId <=0){
		console.log('空指针了')
		return;
	}
	$.ajax({
		url:ajax_host+'adminIdNewPwd.act',
		type : 'post',
		dataType:'JSON',
		data:{
			uid : user.adminId
		},
		success:function(resp){
			
			if(resp.success){
				
				layer.alert('重置成功,密码为a123456');
				getAdminlist();
			}else{
				layer.alert(resp.errmsg);
			}
		}
	})


   });
	
}
function doAdd(){
    $('#doAdd').modal({
        show:true,//展示出来
        moveable:true,//可移动
        rememberPos:true,//记住移动位置
        backdrop:'static'//背景遮罩
	});
	

}
function confirm(){
	var admin = $('#admin').val();
	var pwd = $('#pwd').val();
	var cpwd = $('#cpwd').val();
	var account = $('#account').val();
	var roleid = $('#roleid').val();
	var addressOne = $('#addressOne').val();
	var addressTwo = $('#addressTwo').val();
	var addressThree = $('#addressThree').val();
	console.log(admin);
	console.log(pwd);
	console.log(cpwd);
	console.log(roleid);
	console.log(account);
	console.log(addressOne);
	console.log(addressTwo);
	console.log(addressThree);
	console.log('添加管理员');
	console.log($);
	
	$.ajax({
		url:ajax_host+'addAdmin.act',
		type : 'post',
		dataType:'JSON',
		data:{
			admin : admin,
			pwd : pwd,
			cpwd : cpwd,
			role : roleid,
			account : account,
			addressOne : addressOne,
			addressTwo : addressTwo,
			addressThree : addressThree
		},
		
		success:function(resp){
			if(resp.success){
				layer.alert('添加成功');
				
				getAdminlist();
				$('#doAdd').modal('hide', 'fit');
			}else{
				layer.alert(resp.errmsg);
				$('#doAdd').modal('hide', 'fit');
			}
		}
	})

    
}


function pagenext() {
	if (currPage + 1 > allPage) {
		layer.alert("当前已经是最后一页");
		return;
	}
	
	currPage = currPage + 1;
	getAdminlist();
};

function pageback() {
	if (currPage - 1 == 0) {
		layer.alert("当前已经是第一页");
		return;
	}
	
	currPage = currPage - 1;
	getAdminlist();
};

